package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import bean.Book;
@Repository
public class BookDaoImpl implements BookDao{
	@Autowired
	private JdbcTemplate template;
	@Value(value="insert into books (id,bname,price) values(john_seq01.nextval,?,?)")
	private String addSql;
	@Value(value="update  books set bname=?,price=? where id=?")
	private String updateSql;
	@Value(value="delete books where id=?")
	private String deleteSql;
	@Value(value="select * from books")
	private String findAllSql;
	@Override
	public int addBook(Book book) {
		
		return template.update(addSql,book.getBname(),book.getPrice());
	}
	@Override
	public int updateBook(Book book) {
		return template.update(updateSql,book.getBname(),book.getPrice(),book.getId());
	}
	@Override
	public int deleteBook(int id) {
		return template.update(deleteSql,id);
	}
	@Override
	public List<Book> findAllBooks() {
		
		return template.query(findAllSql, new RowMapper<Book>() {
			@Override
			public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
				Book book=new Book();
				book.setBname(rs.getString("bname"));
				book.setPrice(rs.getDouble("price"));
				book.setId(rs.getInt("id"));
				return book;
			}
		});
	}

}
